/**
 *
 */
package cn.ipenta.jkb.service;

import cn.ipenta.jkb.JkbServiceException;
import cn.ipenta.jkb.entity.TxInfo;
import cn.ipenta.jkb.repository.TxInfoBlockChainSource;
import cn.ipenta.jkb.repository.TxInfoRepository;
import com.alibaba.fastjson.JSON;
import org.slf4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;

import java.util.List;

/**
 * @author ipenta
 * @ClassName: AbstractTxService
 * @Description: TODO
 * @date 2017年4月19日 上午9:07:20
 */
public abstract class AbstractTxService {

	private Logger logger = org.slf4j.LoggerFactory.getLogger(AbstractTxService.class);

	@Autowired
	TxInfoRepository txInfoRepository;

	@Autowired
	protected TxInfoBlockChainSource txInfoBlockChainSource;

	public Boolean createPay(TxInfo txInfo) {
		Boolean isCreated = Boolean.FALSE;

		txInfo.setInfo("无");
		TxInfo entity = txInfoRepository.save(txInfo);
		if (null != entity) {
			String[] txhashAndtime = txInfoBlockChainSource.createInBack(entity).split(",");
			entity.setTxhash(txhashAndtime[0]);
			entity.setTime(txhashAndtime[1]);
			if (null != txInfoRepository.save(entity)){
				isCreated = Boolean.TRUE;
			}
		}


		return isCreated;
	}

	/*****旧版****/

	public Boolean __createPay(TxInfo txInfo) {
		Boolean isCreated = Boolean.FALSE;

		if (!txInfoRepository.exists(Example.of(txInfo))) {
			TxInfo reqTxinfo = txInfoRepository.findOne(Example.of(new TxInfo(txInfo.getSenderId(), txInfo.getType())));
			logger.debug(JSON.toJSONString(reqTxinfo));

			if (reqTxinfo != null) {
				txInfo.setInfo("无");
				Long cost = new Long(reqTxinfo.getNum()) - new Long(txInfo.getNum());
				if (cost >= 0) {
					TxInfo entity = txInfoRepository.save(txInfo);
					if (entity != null) {
						isCreated = Boolean.TRUE;
						String[] txhashAndtime = txInfoBlockChainSource.createInBack(entity).split(",");
						entity.setTxhash(txhashAndtime[0]);
						entity.setTime(txhashAndtime[1]);
						txInfoRepository.save(entity);
					}
				} else {
					throw new JkbServiceException("货币持有不足够");
				}

			}
		}
		return isCreated;
	}


	/**
	 * @param txInfo
	 * @return
	 */
	public Boolean pay(TxInfo txInfo) {
		if (txInfoBlockChainSource.has(txInfo)) {
			throw new JkbServiceException("交易存证已经存在");
		}
		// 查询交易发起者是否有过存证
		TxInfo entity = txInfoBlockChainSource.findByTypeAndReceiverId(txInfo.getType(), txInfo.getSenderId());
		if (entity != null) {
			logger.debug("method:pay:请求货币：{}，拥有货币：{}", txInfo.getNum(), entity.getNum());
			Long cost = new Long(entity.getNum()) - new Long(txInfo.getNum());
			txInfo.setInfo(entity.getInfo());
			if (cost > 0 && txInfoBlockChainSource.create(txInfo) != null) {
				return Boolean.TRUE;
			}
		} else {
			throw new JkbServiceException("交易存证已经存在");
		}

		return Boolean.FALSE;
	}

	public TxInfo findRecordByTypeAndReceiverId(String type, String receiverId) {
		return txInfoBlockChainSource.findByTypeAndReceiverId(type, receiverId);
	}

	public List<TxInfo> findApplyRecordById(String id) {
		TxInfo txInfo = new TxInfo();
		txInfo.setReceiverId(id);
		return txInfoRepository.findAll(Example.of(txInfo));
		// return txInfoBlockChainSource.findRecordByReceiverId(id);
	}

	public List<TxInfo> findPayRecordById(String id) {
		TxInfo txInfo = new TxInfo();
		txInfo.setSenderId(id);
		return txInfoRepository.findAll(Example.of(txInfo));
		// return txInfoBlockChainSource.findRecordBySenderId(id);
	}

}